package com.zhanglijie.base.sync;

import org.omg.PortableServer.THREAD_POLICY_ID;

import com.zhanglijie.base.sync.Lock.MyUnsafe;
import com.zhanglijie.improve.atomic.three.Member;

import sun.misc.Unsafe;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/10/6 0006 17:14
 */
public class UnsafeUseSync {
    public static void main(String[] args) {
        Unsafe unsafe = MyUnsafe.getUnsafe();
        UnsafeUseSync unsafeUseSync = new UnsafeUseSync();

        new     Thread(()->{unsafeUseSync.method1(unsafe);},"t2").start();
        new     Thread(()->{unsafeUseSync.method2(unsafe);},"t2").start();
    }
    public  void method1(Unsafe unsafe){
        unsafe.monitorEnter(this);
        System.out.println("method1==="+Thread.currentThread().getName());
        try{
            Thread.sleep(2000);
        }catch (Exception e){}
        finally {
            unsafe.monitorExit(this);
        }
    }

    public void method2(Unsafe unsafe){
        unsafe.monitorEnter(this);
        System.out.println("method2===="+ Thread.currentThread().getName());
        try{
            Thread.sleep(2000);
        }catch (Exception e){}
        finally {
            unsafe.monitorExit(this);
        }
    }

}
